import re
import time
import traceback
import record_file
import repeat_data
# import pymysql
from watchdog.observers import Observer
from watchdog.events import *


file_path = r'D:\u盾'
path_list = ['已通过信息', '未通过信息', '个人参保证明', '人员信息查询', '单位参保证明', '单位信息查询', '单位征收计划查询']


class Myhandler(FileSystemEventHandler):
    # 修改执行
    def on_modified(self, event):
        pass

    # 创建执行
    def on_created(self, event):
        time.sleep(1)
        # 文件目录
        os.chdir("D:/Users/kk/PycharmProjects/pythonProject/社保/")
        str_path = event.src_path
        print(str_path)
        if re.search('已分析',str_path):
            os.remove(str_path)
        else:
            try:
                # 防止误打开文件，使程序无法读取数据报错
                if re.search('~', str_path):
                    str_path = 'none'
                str_save = ''
                # 遍历目录信息
                for i in range(len(path_list)):
                    # 筛选出任务类型
                    if re.search(path_list[i], str_path):
                        str_save = path_list[i]
                # 路径数据分解
                accpet = str_path.split('\\')
                # 已通过判断
                if str_save == path_list[0]:
                    file_name = accpet[-1]
                    company_name = file_name.split('_')[0]
                    record_type = '已通过信息-'+file_name.split('_')[1]
                    # 调用文件上传数据库接口

                    repeat_data.data_return(company_name, str_path, record_type)
                    record_file.run_write(company_name, str_path, '已通过信息', '0')
                # 未通过信息
                elif str_save == path_list[1]:
                    file_name = accpet[-1]
                    company_name = file_name.split('_')[0]
                    record_type = '未通过信息-' + accpet[-2]
                    # 调用文件上传数据库接口
                    repeat_data.data_return(company_name, str_path, record_type)
                    record_file.run_write(company_name, str_path, '未通过信息','0')
                # 个人参保证明
                elif str_save == path_list[2]:
                    file_name = accpet[-1]
                    id_number = file_name.split('-')[0]
                    people_name = file_name.split('-')[1]
                    company_name = accpet[-2]
                    record_type = accpet[-4]

                    print(company_name, record_type, file_name, id_number,people_name)
                    record_file.run_write(company_name, str_path, record_type, '0', file_name=file_name,
                                          id_number=id_number,people_name=people_name)

                # 人员信息查询
                elif str_save == path_list[3]:
                    #
                    #
                    # 待建
                    #
                    #
                    #
                    #
                    pass
                # 单位参保证明
                elif str_save == path_list[4]:
                    file_name = accpet[-1]
                    company_name = re.sub(r'【\d】','',file_name).split('-')[0]
                    record_type = accpet[-3]

                    record_file.run_write(company_name, str_path, record_type, '0')
                # 单位信息查询
                elif str_save == path_list[5]:
                    file_name = accpet[-1]
                    company_name = file_name.split('-')[0]
                    record_type = accpet[-3]
                    record_file.run_write(company_name, str_path, record_type, '0')
                # 单位征收计划查询
                elif str_save == path_list[6]:
                    file_name = accpet[-1]
                    company_name = re.sub(r'【\d】', '', file_name).split('-')[0]
                    record_type = accpet[-3]
                    print(company_name, str_path, record_type)
                    record_file.run_write(company_name, str_path, record_type, '0')

            except Exception as e:
                print(e)
                print(traceback.format_exc())


#
# def monitor_ss(file_path):
#     evet_handler = Myhandler()
#     observer = Observer()
#     observer.schedule(evet_handler, file_path, recursive=True)
#     observer.start()
#
#     try:
#         while True:
#             time.sleep(1)
#     except KeyboardInterrupt:
#         observer.stop()
#     observer.join()


if __name__ == '__main__':

    path = file_path
    evet_handler = Myhandler()
    observer = Observer()
    observer.schedule(evet_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
